/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fourbox;

import java.io.File;
import java.io.Serializable;
import java.util.Date;

/**
 *
 * @author hartono
 */
public class Metafile implements Serializable {

    // directory
    // file name
    // isFile
    // check sum
    // last modified
    public String directory;
    public String fileName;
    public boolean isFile;
    public long lastModified;
    public int status;
    public long size;
    public static final int OK = 1;
    public static final int DELETED = 0;
    public static final int UPDATED = 2;
    public static final int NEW = 3;
    public Date lastModifiedDate;

    private Metafile() {
    }

    public static Metafile createMetafile(File file) {
        Metafile retval = new Metafile();
        retval.lastModified = file.lastModified();
        retval.fileName = file.getName();
        retval.isFile = file.isFile();
        retval.directory = file.getPath();
        retval.lastModifiedDate = new Date(retval.lastModified);
        retval.size = file.length();

        retval.status = OK;

        return retval;
    }

    public static Metafile createMetafile(File file, long last) {
        Metafile retval = new Metafile();
        retval.lastModified = last;
        retval.fileName = file.getName();
        retval.isFile = file.isFile();
        retval.directory = file.getPath();
        retval.lastModifiedDate = new Date(retval.lastModified);
        retval.size = file.length();


        return retval;
    }

    public static Metafile createMetafile(String contextPath, File file) {

        try {
            File contextFile = new File(contextPath);
            String context = contextFile.toString().replace("/", "\\");

            int index = file.toString().lastIndexOf(context) + context.length() + 1;

            String relativePath = file.toString().substring(index);
            Metafile retval = new Metafile();
            retval.lastModified = file.lastModified();
            retval.fileName = file.getName();
            retval.isFile = file.isFile();
            retval.directory = relativePath;
            retval.lastModifiedDate = new Date(retval.lastModified);

            retval.size = file.length();

            retval.status = OK;


            return retval;
        } catch (StringIndexOutOfBoundsException ex) {
            File contextFile = new File(contextPath);
            String context = contextFile.toString().replace("/", "\\");

            int index = file.toString().lastIndexOf(context) + context.length();

            String relativePath = file.toString().substring(index);
            Metafile retval = new Metafile();
            retval.lastModified = file.lastModified();
            retval.fileName = file.getName();
            retval.isFile = file.isFile();
            retval.directory = relativePath;
            retval.lastModifiedDate = new Date(retval.lastModified);

            retval.size = file.length();

            retval.status = OK;


            return retval;
        }
    }

    public static File createFileFromMetafileServer(String username, Metafile metafile) {
        File retval = new File("Repository/" + username + "/" + metafile.directory);
        retval.setLastModified(metafile.lastModified);        
        return retval;
    }

    public static File createFileFromMetafileClient(String workingPath, Metafile metafile) {
        File retval = new File(workingPath + "/" + metafile.directory);
        retval.setLastModified(metafile.lastModified);        
        return retval;
    }

    @Override
    public boolean equals(Object obj) {
        return directory.equals(((Metafile) obj).directory) && fileName.equals(((Metafile) obj).fileName)
                && (isFile == ((Metafile) obj).isFile) ;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        if (isFile) {
            sb.append("[File]  \t");
        } else {
            sb.append("[Folder]\t");
        }

//        sb.append(fileName + "\t");
        sb.append(directory + "\t");
        sb.append(lastModifiedDate + "\t");
        sb.append(size + "\t");

        if (status == OK) {
            sb.append("OK");
        } else if (status == DELETED) {
            sb.append("DELETED");
        } else if (status == UPDATED) {
            sb.append("UPDATED");
        } else if (status == NEW) {
            sb.append("NEW");
        }
        sb.append('\t');
        return sb.toString();
    }
}
